# 设置项目所需的cmake的最低版本
cmake_minimum_required(VERSION 3.5)

project(LVT_ScanDev LANGUAGES CXX VERSION 1.0)

# 设置c++11标准
set(CMAKE_CXX_STANDARD 11)

# CMAKE_CXX_STANDARD设置的标准版本是否强制要求
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 阻止msvc unicode警告，
set(MSVC_AUX_FLAG "/wd4819;/source-charset:utf-8;/execution-charset:utf-8") # 代码文件和编译后的字符串都使用utf-8编码
add_compile_options("$<$<AND:$<C_COMPILER_ID:MSVC>,$<VERSION_GREATER:$<C_COMPILER_VERSION>,18.99>>:${MSVC_AUX_FLAG}>")
add_compile_options("$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,18.99>>:${MSVC_AUX_FLAG}>")

# 编译信息
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # 以json文件保存编译命令行, 生成的compile_commands.json 在构建目录，仅makefile和ninja构建器可用

# set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "" FORCE) # 强制显示makefile信息

# Only generate Debug and Release configuration types.
# CMAKE_CONFIGURATION_TYPES主要用于复合配置生成器（例如生成Visual Studio工程，默认的构建配置）
set(CMAKE_CONFIGURATION_TYPES Debug Release)
set(CONFIGURATION_TYPES "$<IF:$<CONFIG:Debug>,Debug,Release>")

# ######################################################################
if(CMAKE_SYSTEM_NAME MATCHES "Windows") # 判断平台
    set(CMAKE_INSTALL_PREFIX "${PROJECT_BINARY_DIR}/build")
    set(OS_WINDOWS 1)
    add_compile_definitions(OS_WIN OS_WINDOWS)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    set(OS_LINUX 1)
    add_compile_definitions(OS_LINUX)
else()
endif()

# ############################输出目录设定##################################
# CMAKE_BINARY_DIR 是顶层构建目录
# CMAKE_CURRENT_BINARY_DIR 是当前构建目录
# PROJECT_BINARY_DIR : 最近的包含project()命令的编译目录。
# CMAKE_ARCHIVE_OUTPUT_DIRECTORY：默认存放静态库的文件夹位置；
# CMAKE_LIBRARY_OUTPUT_DIRECTORY：默认存放动态库的文件夹位置；
# LIBRARY_OUTPUT_PATH：默认存放库文件的位置，如果产生的是静态库并且没有指定
# CMAKE_RUNTIME_OUTPUT_DIRECTORY：存放可执行软件的目录
# set(CMAKE_output "${CMAKE_BINARY_DIR}/${CONFIGURATION_TYPES}")
# message("???????????????? ${CMAKE_BINARY_DIR}")
# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/lib)
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/lib)
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bin)

# # set output directories for all builds (Debug, Release, etc.)
# foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
# string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
# set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/lib )
# set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/lib )
# set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/bin )
# endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )

# ########################  公共的库 #################################
if(OS_WINDOWS)
    set(logger_plus_test_lib_path "D:/logger_plus_test_lib" CACHE PATH "logger_plus_test library path.")
endif()

find_package(logger_plus_test_lib REQUIRED COMPONENTS ALL PATHS "${logger_plus_test_lib_path}")

# fmt库
# include(fmtlib.cmake)

# spdlog库
# set(<variable> <value>... CACHE <type> <docstring> [FORCE])
# set(SPDLOG_FMT_EXTERNAL ON CACHE BOOL "" FORCE) # 强制spdlog使用外部库
# set(SPDLOG_BUILD_SHARED OFF CACHE BOOL "" FORCE) # 强制禁止spdlog构建成共享库
# include(spdlog.cmake)

# target_include_directories(spdlog PUBLIC fmt)
# target_link_libraries(spdlog INTERFACE fmt)
# target_link_directories(spdlog INTERFACE fmt)

# nlohmann-json 库
# include(nlohmann-json.cmake)

# googletest 库
# include(googletest.cmake)

# ########################  BLL SLD光源封装库 #################################
file(GLOB srcfiles src/*.cpp)
add_library(LVT_BLLDev OBJECT ${srcfiles})
target_compile_options(LVT_BLLDev PRIVATE "$<$<OR:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>:/EHa>")
target_include_directories(LVT_BLLDev PRIVATE logger_plus_test_lib)
target_link_libraries(LVT_BLLDev PRIVATE logger_plus_test_lib)
target_include_directories(LVT_BLLDev PUBLIC "${CMAKE_CURRENT_LIST_DIR}/src")

# ########################  BLL SLD光源封装库 测试 #################################
file(GLOB testsrcfiles test/*.cpp)
list(SORT testsrcfiles)

macro(CommonIncludeLink target_name)
    target_include_directories(${target_name} PRIVATE LVT_BLLDev)
    target_link_libraries(${target_name} PRIVATE LVT_BLLDev)
    target_include_directories(${target_name} PRIVATE logger_plus_test_lib)
    target_link_libraries(${target_name} PRIVATE logger_plus_test_lib)
endmacro()

foreach(src_name ${testsrcfiles})
    get_filename_component(src_name ${src_name} NAME_WLE)
    # message("${src_name}")
    add_executable(
        ${src_name}
        ${CMAKE_CURRENT_LIST_DIR}/test/${src_name}.cpp
    )
    CommonIncludeLink(${src_name})
endforeach()